﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Drawing;


namespace LoginCompositeControl
{
    public class CompositeControl : Control, INamingContainer
    {
        private TextBox txbUserName;
        private TextBox txbPassword;
			  
        public string Password
        {
            get
            {
                //	необходимо убедиться, что подчиненные контролы существуют. В противном 
                //	случае они будут созданы			
                EnsureChildControls();
								return txbPassword.Text;
            }
        }
        public string UserName
        {
            get
            {
                EnsureChildControls();
								return txbUserName.Text;
            }
        }

        public event EventHandler<CompositeControlEventArgs> ButtonClicked;
			
        protected override void CreateChildControls()
        {

            HtmlControl mainFieldset = new HtmlGenericControl("fieldset");
            mainFieldset.Attributes["class"] = "compositeControl";

            HtmlControl legent = new HtmlGenericControl("legend") {InnerText = "CompositeControl" };
            mainFieldset.Controls.Add(legent);

					HtmlControl firstRowDiv = new HtmlGenericControl("Div");
        	firstRowDiv.Attributes["class"] = "rowOfCompositeControl";
					HtmlControl secondRowDiv = new HtmlGenericControl("Div");
					secondRowDiv.Attributes["class"] = "rowOfCompositeControl";
					HtmlControl thirdRowDiv = new HtmlGenericControl("Div");
					thirdRowDiv.Attributes["class"] = "rowOfCompositeControl";


        	txbUserName = new TextBox {ID = "txbUserName"};

            RequiredFieldValidator requiredFieldTxbUserName = new RequiredFieldValidator
                                                                                        {
                                                                                            ValidationGroup = "CompositeControl",
                                                                                            ControlToValidate = "txbUserName",
                                                                                            Text = "*",
                                                                                            ErrorMessage = "Empty field of User Name",
                                                                                            ForeColor = Color.Red
                                                                                        };

        	var spanUserName = new HtmlGenericControl("span") {InnerText = "User Name"};

        	firstRowDiv.Controls.Add(txbUserName);
					firstRowDiv.Controls.Add(requiredFieldTxbUserName);
					firstRowDiv.Controls.Add(spanUserName);


					txbPassword = new TextBox { TextMode = TextBoxMode.Password, ID = "txbPassword" };

                    RequiredFieldValidator requiredFieldTxbPassword = new RequiredFieldValidator
                    {
                        ValidationGroup = "CompositeControl",
                        ControlToValidate = "txbPassword",
                        Text = "*",
                        ErrorMessage = "Empty field of password",
                        ForeColor = Color.Red
                    };

        	var spanPassword = new HtmlGenericControl("span") {InnerText = "Password"};

        	secondRowDiv.Controls.Add(txbPassword);
					secondRowDiv.Controls.Add(requiredFieldTxbPassword);
					secondRowDiv.Controls.Add(spanPassword);


					ValidationSummary compositeControlValidationSummary=new ValidationSummary();

        	compositeControlValidationSummary.ValidationGroup = "CompositeControl";
        	compositeControlValidationSummary.ShowMessageBox = true;
        	compositeControlValidationSummary.ShowSummary = false;
        	compositeControlValidationSummary.DisplayMode = ValidationSummaryDisplayMode.BulletList;

          Button submit = new Button();
					submit.Text = "Submit";
					submit.Click += new EventHandler(SubmitInnerClicked);
                    submit.Attributes["onclick"] = "return submitOfCompositeControlClicked(event);";
					submit.Attributes["class"] = "submitOfCompositeControl";
        	submit.ValidationGroup = "CompositeControl";

					thirdRowDiv.Controls.Add(compositeControlValidationSummary);
						thirdRowDiv.Controls.Add(submit);


                        mainFieldset.Controls.Add(firstRowDiv);
                        mainFieldset.Controls.Add(secondRowDiv);
                        mainFieldset.Controls.Add(thirdRowDiv);

                        Controls.Add(mainFieldset);

        }
				private void SubmitInnerClicked(object sender, EventArgs fe)
				{
					var Temp = ButtonClicked;
					if (Temp != null)
						Temp(this, new CompositeControlEventArgs(Password, UserName));
				}

				//add Css fle
				protected void AddCSSFile()
				{
					bool linkIncluded = false;
					foreach (Control c in Page.Header.Controls)
					{
						if (c.ID == "StyleOfCompositeControl")
						{
							linkIncluded = true;
						}
					}
					if (!linkIncluded)
					{
						HtmlGenericControl csslink = new HtmlGenericControl("link");
						csslink.ID = "StyleOfCompositeControl";
						csslink.Attributes.Add("href",
																	 Page.ClientScript.GetWebResourceUrl(this.GetType(),
																																			 "LoginCompositeControl.Styles.StyleOfCompositeControl.css"));
						csslink.Attributes.Add("type", "text/css");
						csslink.Attributes.Add("rel", "stylesheet");
						Page.Header.Controls.Add(csslink);

					}
				}

				//add script to page
				protected void AddScriptFile()
				{
					if (!Page.ClientScript.IsClientScriptIncludeRegistered("ScriptOfCompositeControl"))
					{
						// When pre-rendering, add in external JavaScript file
						Page.ClientScript.RegisterClientScriptInclude("ScriptOfCompositeControl",
													 Page.ClientScript.GetWebResourceUrl(this.GetType(),
																											 "LoginCompositeControl.Scripts.ScriptOfCompositeControl.js"));
					}
				}

				protected override void OnPreRender(EventArgs e)
				{
					AddCSSFile();
					AddScriptFile();
				
					base.OnPreRender(e);
				} 
    }
}
